<?php


namespace Biz\service\wikiRanking;


use Biz\model\wiki\WikiRankingBrand;
use Biz\model\wiki\WikiRankingHot;
use Biz\model\wiki\WikiRankingPublicPraise;
use Biz\model\wiki\WikiRankingPic;
use Biz\service\wiki\Wiki as sWiki;
use Doraemon\tools\BlockType;
use Doraemon\tools\HhzEnv;
use Doraemon\tools\ImgUrl;


/**
 * 榜单后台审核相关
 * Class AdmRanking
 * @package Biz\service\wikiRanking
 */
class AdmRanking
{

    const TYPE_ARR = [
        [
            'type' => self::TYPE_HOT,
            'name' => '热度榜'
        ], [
            'type' => self::TYPE_PUBLIC,
            'name' => '口碑榜'
        ], [
            'type' => self::TYPE_BRAND,
            'name' => '品牌榜'
        ]
    ];

    const TYPE_HOT = 1;
    const TYPE_BRAND = 3;
    const TYPE_PUBLIC = 2;

    const BRAND_TYPE_DEFAULT = 0;
    const BRAND_TYPE_EXPIRE = 1;
    const BRAND_TYPE_COMMON = 2;
    const BRAND_TYPE_BEST = 3;
    const BRAND_TYPE_SUPER = 4;
    const BRAND_TYPE = [
        self::BRAND_TYPE_EXPIRE => '到期品牌',
        self::BRAND_TYPE_COMMON => '普通认证品牌',
        self::BRAND_TYPE_BEST => '精选品牌',
        self::BRAND_TYPE_SUPER => '超级品牌',
    ];


    /*格式化热榜数据
     *
     * */
    public static function formateHotList($list, $lastMap)
    {
        $lastArr = [];
        if ($lastMap) {
            $lastList = WikiRankingHot::getListByWhere($lastMap);
            foreach ($lastList as $row) {
                $lastArr[$row['ranking_id'] . '_' . $row['basic_id']] = ['continue_week' => $row['continue_week'], 'ranking_day' => $row['ranking_day']];
            }
        }
        if ($list && is_array($list)) {
            $basic_ids = array_column($list, 'basic_id');
            $basic_info = sWiki::getWikiListByBasicIds($basic_ids);
            foreach ($list as &$val) {
                $val['wiki_info'] = $basic_info[$val['basic_id']];
                $val['cover_img'] = $basic_info[$val['basic_id']]['cover_img'];
                $val['basic_title'] = $basic_info[$val['basic_id']]['basic_title'];
                $val['brand_name'] = $basic_info[$val['basic_id']]['brand_info']['name'];
                $val['brand_type'] = self::BRAND_TYPE[$basic_info[$val['basic_id']]['brand_info']['brand_type']];
                $val['brand_logo'] = $basic_info[$val['basic_id']]['brand_info']['logo'];
                $val['h5_wiki_url'] = HhzEnv::detectShopDomain() . 'shop/goods-detail/wiki?wiki_id=' . $basic_info[$val['basic_id']]['id'] . '&no-pc-redirect=true';
                $val['wiki_img'] = $val['basic_pic_id'] ? ImgUrl::getUrl(ImgUrl::APPTYPE_OP, $val['basic_pic_id'],
                    ImgUrl::IMAGE_SIZE_OP_ORI) : '';
                if ($lastMap) {
                    $key = $val['ranking_id'] . '_' . $val['basic_id'];
                    if (isset($lastArr[$key])) {
                        $val['continue_week'] = $lastArr[$key]['continue_week'];
                        $val['last_ranking_day'] = $lastMap['ranking_day'];
                    } else {
                        $val['continue_week'] = 0;
                        $val['last_ranking_day'] = $lastMap['ranking_day'];
                    }
                } else {
                    $val['continue_week'] = $val['continue_week'];
                    $val['last_ranking_day'] = $val['ranking_day'];
                }
            }
        }
        return $list;


    }

    /*格式化品牌数据
    *
    * */
    public static function formateBrandList($list, $lastMap)
    {
        $lastArr = [];
        if ($lastMap) {
            $lastList = WikiRankingBrand::getListByWhere($lastMap);
            foreach ($lastList as $row) {
                $lastArr[$row['ranking_id'] . '_' . $row['brand_id']] = ['continue_week' => $row['continue_week'], 'ranking_day' => $row['ranking_day']];
            }
        }
        if ($list && is_array($list)) {
            $basic_ids = array_column($list, 'basic_id');
            $basic_info = sWiki::getWikiListByBasicIds($basic_ids);
            foreach ($list as &$val) {
                $val['wiki_info'] = $basic_info[$val['basic_id']];
                $val['cover_img'] = $basic_info[$val['basic_id']]['cover_img'];
                $val['basic_title'] = $basic_info[$val['basic_id']]['basic_title'];
                $val['brand_name'] = $basic_info[$val['basic_id']]['brand_info']['name'];
                $val['brand_type'] = self::BRAND_TYPE[$basic_info[$val['basic_id']]['brand_info']['brand_type']];
                $val['brand_logo'] = $basic_info[$val['basic_id']]['brand_info']['logo'];
                $val['h5_wiki_url'] = HhzEnv::detectShopDomain() . 'shop/goods-detail/wiki?wiki_id=' . $basic_info[$val['basic_id']]['id'] . '&no-pc-redirect=true';

                if ($lastMap) {
                    $key = $val['ranking_id'] . '_' . $val['brand_id'];
                    if (isset($lastArr[$key])) {
                        $val['continue_week'] = $lastArr[$key]['continue_week'];
                        $val['last_ranking_day'] = $lastMap['ranking_day'];
                    } else {
                        $val['continue_week'] = 0;
                        $val['last_ranking_day'] = $lastMap['ranking_day'];
                    }
                } else {
                    $val['continue_week'] = $val['continue_week'];
                    $val['last_ranking_day'] = $val['ranking_day'];
                }
            }
        }
        return $list;
    }

    /*格式化口碑数据
    *
    * */
    public static function formatePublicPraiseList($list, $lastMap)
    {
        $lastArr = [];
        if ($lastMap) {
            $lastList = WikiRankingPublicPraise::getListByWhere($lastMap);
            foreach ($lastList as $row) {
                $lastArr[$row['ranking_id'] . '_' . $row['basic_id']] = ['continue_week' => $row['continue_week'], 'ranking_day' => $row['ranking_day']];
            }
        }
        if ($list && is_array($list)) {
            $obj_ids = array_column($list, 'obj_id') ?: [];

            if ($obj_ids && is_array($obj_ids)) {
                $content_info = Ranking::getContentInfo($obj_ids);
            }
            $basic_ids = array_column($list, 'basic_id');
            $basic_info = sWiki::getWikiListByBasicIds($basic_ids);
            foreach ($list as &$val) {
                $val['wiki_info'] = $basic_info[$val['basic_id']];
                $val['cover_img'] = $basic_info[$val['basic_id']]['cover_img'];
                $val['basic_title'] = $basic_info[$val['basic_id']]['basic_title'];
                $val['brand_name'] = $basic_info[$val['basic_id']]['brand_info']['name'];
                $val['brand_type'] = self::BRAND_TYPE[$basic_info[$val['basic_id']]['brand_info']['brand_type']];
                $val['brand_logo'] = $basic_info[$val['basic_id']]['brand_info']['logo'];
                $val['h5_wiki_url'] = HhzEnv::detectShopDomain() . 'shop/goods-detail/wiki?wiki_id=' . $basic_info[$val['basic_id']]['id'] . '&no-pc-redirect=true';
                $val['wiki_img'] = $val['basic_pic_id'] ? ImgUrl::getUrl(ImgUrl::APPTYPE_OP, $val['basic_pic_id'],
                    ImgUrl::IMAGE_SIZE_OP_ORI) : '';

                if ($val['obj_type'] == BlockType::TYPE_PHOTO) {
                    $val['content_desc'] = $content_info[$val['obj_id']]['photo']['photo_info']['remark'];
                    $val['content_pic'] = $content_info[$val['obj_id']]['photo']['photo_info']['image_list'][0]['pic_url'];
                    $val['h5_note_url'] = 'https://m.haohaozhu.cn/community/note-share/detail?note_id=' . $val['obj_id'];
                } else {
                    $val['content_desc'] = $content_info[$val['obj_id']]['blank']['blank_info']['title'];
                    $val['content_pic'] = $content_info[$val['obj_id']]['blank']['blank_info']['cover_pic_url'];
                    $val['h5_note_url'] = 'https://m.haohaozhu.cn/blank/' . $val['obj_id'] . '.html?preview=1';
                }
                $val['obj_type'] = $val['obj_type'];
                if ($lastMap) {
                    $key = $val['ranking_id'] . '_' . $val['basic_id'];
                    if (isset($lastArr[$key])) {
                        $val['continue_week'] = $lastArr[$key]['continue_week'];
                        $val['last_ranking_day'] = $lastMap['ranking_day'];
                    } else {
                        $val['continue_week'] = 0;
                        $val['last_ranking_day'] = $lastMap['ranking_day'];
                    }
                } else {
                    $val['continue_week'] = $val['continue_week'];
                    $val['last_ranking_day'] = $val['ranking_day'];
                }
            }
        }
        return $list;

    }



    //获取basic_pic_id
    public static function getBasicPicIdsByRankingList($RankingList)
    {
        $result = [];
        if($RankingList && is_array($RankingList)){
            $basic_ids  = array_column($RankingList , 'basic_id');
            $basicPicList = WikiRankingPic::selectByWhere(['basic_id' => $basic_ids , 'pic_status' => WikiRankingPic::STATUS_NORMAL] , ['basic_id' ,'basic_pic_id']);
            if($basicPicList && is_array($basicPicList)){
                foreach($basicPicList as $val){
                    $result[$val['basic_id']] = $val['basic_pic_id'];
                }
            }
        }
        return  $result;

    }


}